package org.datacoder.geocoding.datastructures.rectangles

import org.datacoder.geocoding.datastructures.geoshapes.*
import org.datacoder.geocoding.datastructures.util.*
import org.datacoder.geocoding.jurisdiction.*

class LayeredSegmentTreeNode {

    LayeredSegmentTreeNode left = null
    LayeredSegmentTreeNode right = null
    double leftValue
    double rightValue

    Set<Jurisdiction> jurisdictions
    SegmentTree segmentTree

    public LayeredSegmentTreeNode(){
        jurisdictions = new HashSet<Jurisdiction>()
    }
    public boolean containsPoint(double point){
        return ((leftValue <= point) && (point <= rightValue))
    }
    public void addJurisdiction(Jurisdiction juris){
        jurisdictions.add(juris)
    }
    public void lock(){
        if(!jurisdictions.isEmpty()){
            segmentTree = new SegmentTree(jurisdictions, GeoShape.LAT)
            jurisdictions.clear()
        }
        jurisdictions = null
    }
    public Set<Jurisdiction> findRectanglesContainingPoint(GeoPoint gp){
        if(segmentTree != null){
            return jurisdictions = segmentTree.findIntervalsContainingPoint(gp.lat)
        }else{
            return new HashSet<Jurisdiction>()
        }
    }
    
}

